﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/jquery-ui.min.js" />
///<reference path="../../scripts/kooboo/kooboo.js" />
///<reference path="../../scripts/kooboo/kooboo.html.js" />
///<reference path="../../scripts/kooboo/kooboo.webcontrols.js" />
///<reference path="../../scripts/kooboo/kooboo.mvcvalidation.js" />
///<reference path="../../scripts/jquery.formatCurrency-1.4.0.js" />
///<reference path="../../scripts/jquery.formatCurrency.all.js" />


var SaleItemEditIndex = -1;

//toolbar
function showAddButton() {
    $(".add-variation").removeClass("hide");
    $(".variation-form").addClass("hide");
}

function hideAddButton() {
    $(".add-variation").addClass("hide");
    $(".variation-form").removeClass("hide");
}


//EditorForSaleItem
function EditorForSaleItem(index) {
    var form = $(".variation-form");
    form.cleanError();

    hideAddButton();

    if (index == null) {//new
        for (var name in kooboo.webcontrols) {
            var ui = kooboo.webcontrols[name];
            if (ui.id && (ui.id.indexOf("CustomField") < 0)) {
                ui.reset();
            }
        }

        form.find(".edit-template-saleitem-sku").val("");
        form.find(".edit-template-saleitem-purchaseprice").val("0");
        form.find(".edit-template-saleitem-retailprice").val("0");
        form.find(".edit-template-saleitem-stock").val("0");
        form.find(".edit-template-saleitem-deliverydays").val("0");

        SaleItemEditIndex = SaleItems.length;

        SaleItems[SaleItemEditIndex] = { Id: -1, ProductVariations: [] };

    } else {

        SaleItemEditIndex = index;
        var editItem = SaleItems[index];
        for (var i = 0; i < editItem.ProductVariations.length; i++) {
            var item = editItem.ProductVariations[i];
            for (var name in kooboo.webcontrols) {
                var ui = kooboo.webcontrols[name];
                if (ui.id && (ui.id.indexOf("CustomField") < 0)) {
                    if (ui.id == item.VariationId) {
                        ui.val(item.Value);
                    }
                }
            }
        }

        form.find(".edit-template-saleitem-sku").val(editItem.SKU);
        form.find(".edit-template-saleitem-purchaseprice").val(editItem.PurchasePrice);
        form.find(".edit-template-saleitem-retailprice").val(editItem.RetailPrice);
        form.find(".edit-template-saleitem-stock").val(editItem.Stock);
        form.find(".edit-template-saleitem-deliverydays").val(editItem.DeliveryDays);


    }
}

//UpdateSaleItem
function UpdateSaleItem() {
    var form = $(".variation-form");


    var editItem = SaleItems[SaleItemEditIndex];

    for (var name in kooboo.webcontrols) {
        var ui = kooboo.webcontrols[name];
        if (ui.id && (ui.id.indexOf("CustomField") < 0)) {
            var updateItem = null;
            for (var i = 0; i < editItem.ProductVariations.length; i++) {
                var item = editItem.ProductVariations[i];
                if (ui.id == item.VariationId) {
                    updateItem = item;
                    break;
                }
            }

            if (updateItem == null) {
                updateItem = {};
                updateItem.Id = -1;
                updateItem.VariationId = ui.id;
                editItem.ProductVariations.push(updateItem);
            }

            updateItem.Value = ui.val();
        }
    }



    editItem.SKU = form.find(".edit-template-saleitem-sku").val();
    editItem.PurchasePrice = form.find(".edit-template-saleitem-purchaseprice").val();
    editItem.RetailPrice = form.find(".edit-template-saleitem-retailprice").val();
    editItem.Stock = form.find(".edit-template-saleitem-stock").val();
    editItem.DeliveryDays = form.find(".edit-template-saleitem-deliverydays").val();

    RenderSaleItem(SaleItemEditIndex);

    SaleItemEditIndex = -1;

}

//RenderSaleItem

function RenderSaleItem(index) {
    var container = $("#saleitem-container");

    var template = container.find("tr[index=" + index + "]");
    if (template.size() == 0) {
        template = $("<tr>"
        //+ "<td class='row-assembly'></td><td class='row-sku'></td><td class='row-purchaseprice'></td><td class='row-retailprice'></td><td class='row-stock'></td><td class='row-deliverydays'></td>"
                + "<td class='row-assembly'></td><td class='row-sku'></td><td class='row-purchaseprice'></td><td class='row-retailprice'></td><td class='row-deliverydays'></td>"
                + "<td class='action'><a class='o-icon edit' href='#'>Edit</a></td>"
                + "<td class='action'><a class='o-icon remove' href='#'>Remove</a></td>"
                + "</tr>");

        container.append(template);

        //todo: edit/remove event

        template.find(".remove").click(function (e) {
            e.preventDefault();
            $(this).parent().parent().remove();
            SaleItems[index] = null;
            showAddButton();
        });


        template.find(".edit").click(function (e) {
            e.preventDefault();
            EditorForSaleItem(index);
        });

        template.attr("index", index);
    }

    var item = SaleItems[index];

    //render product variations
    var assembly = [];
    var prefix = "SaleItems[" + index + "]";

    var td_assembly = template.find(".row-assembly");

    td_assembly.empty();

    var saleItem_index = kooboo.html.hidden("SaleItems.index", index);
    var saleItem_id = kooboo.html.hidden(prefix + ".Id", item.Id);
    td_assembly.append(saleItem_index);
    td_assembly.append(saleItem_id);


    for (var i = 0; i < item.ProductVariations.length; i++) {
        var variation_prefix = prefix + ".ProductVariations[" + i + "]";
        var variation = item.ProductVariations[i];
        assembly.push(variation.Value);

        var item_index = kooboo.html.hidden(prefix + ".ProductVariations.index", i);
        var id = kooboo.html.hidden(variation_prefix + ".Id", variation.Id);
        var variationId = kooboo.html.hidden(variation_prefix + ".Variation.Id", variation.VariationId);
        var value = kooboo.html.hidden(variation_prefix + ".Value", variation.Value);

        td_assembly.append(item_index);
        td_assembly.append(id);
        td_assembly.append(variationId);
        td_assembly.append(value);
    }


    td_assembly.append(assembly.join("-"));


    var td_row_purchaseprice = template.find(".row-purchaseprice");
    td_row_purchaseprice.empty();

    td_row_purchaseprice.append(kooboo.html.hidden(prefix + ".PurchasePrice", item.PurchasePrice));
    td_row_purchaseprice.append(kooboo.html.span(item.PurchasePrice).formatCurrency({ region: Culture }));


    var td_row_purchaseprice = template.find(".row-retailprice");
    td_row_purchaseprice.empty();

    td_row_purchaseprice.append(kooboo.html.hidden(prefix + ".RetailPrice", item.RetailPrice));
    td_row_purchaseprice.append(kooboo.html.span(item.RetailPrice).formatCurrency({ region: Culture }));

    var td_row_purchaseprice = template.find(".row-sku");
    td_row_purchaseprice.empty();

    td_row_purchaseprice.append(kooboo.html.hidden(prefix + ".SKU", item.SKU));
    td_row_purchaseprice.append(item.SKU);

    //var td_row_purchaseprice = template.find(".row-stock");
    //td_row_purchaseprice.empty();

    //td_row_purchaseprice.append(kooboo.html.hidden(prefix + ".Stock", item.Stock));
    //td_row_purchaseprice.append(item.Stock);

    var td_row_purchaseprice = template.find(".row-deliverydays");
    td_row_purchaseprice.empty();

    td_row_purchaseprice.append(kooboo.html.hidden(prefix + ".DeliveryDays", item.DeliveryDays));
    td_row_purchaseprice.append(item.DeliveryDays);



    container.find("tr").each(function (i, row) {
        if (i % 2 == 0) {
            $(row).removeClass("even");
        } else {
            $(row).addClass("even");
        }
    });
}

//Event

$(function () {

    $(".datetimepicker").datepicker();

    $(".add-variation").click(function (e) {
        ;
        e.preventDefault();
        $(".variation-form").removeClass("hide");
        EditorForSaleItem(null);
    });

    $(".save-variation").click(function (e) {
        e.preventDefault();
        if ($(".variation-form").mvcvalidate()) {

            UpdateSaleItem();

            showAddButton();

            $(".variation-form").cleanError();
        }
    });

    $(".cancel-variation").click(function (e) {
        e.preventDefault();
        $(".variation-form").cleanError();
        showAddButton();
    });

    for (var i = 0; i < SaleItems.length; i++) {
        RenderSaleItem(i);
    }

    EditorForSaleItem(null);
});

  
